/**
 * 地图操作工具类
 * 与业务无关的地图操作方法
 */

import mapConstants from "./mapConstants";
import MapUtils from "./mapUtils";
export default class MapTool {
    constructor() {}

    async initMapView(container, { zoom }) {
        let [Map, MapView, TileLayer, esriConfig] = await MapUtils.loadModules(["esri/Map", "esri/views/MapView", "esri/layers/TileLayer", "esri/config"]);
        const layer = new TileLayer(await MapUtils.getMapServerUrl(), null);

        let map = new Map({
            layers: [layer],
        });
        let mapView = new MapView({
            container: container,
            map: map,
            zoom: zoom || 4,
        });
        this.map = mapView;
        return mapView;
    }

    /** 创建地图标记 */
    async addMarker(marker) {
        this.map.graphics.add(marker);
    }

    /** 创建带label的地图标记 */
    async addLabelIconMarker(markerGroup) {
        markerGroup.forEach(m => {
            this.map.graphics.add(m);
        });
    }

    /** 设置中心点 */
    async setCenter(point, level) {
        const center = await MapUtils.buildPoint(point);
        let newCenter = await MapUtils.transformCoordinate(center, this.map.center.spatialReference);
        this.map.center = newCenter;
        this.setLevel(level);
    }

    /** 设置地图级别 */
    setLevel(level) {
        this.map.zoom = level;
    }

    /** 清除所有标记 */
    clearAllMarker() {
        this.map.graphics.removeAll();
    }

    /** 清除指定标记 */
    removeMarker(marker) {
        this.map.graphics.remove(marker);
    }
}
